package com.homework.application.videoplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    String FilePath="";
    EditText pathInput;
    Button bn = null;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bn = (Button) findViewById(R.id.button_video_search);
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取用户输入
                String pathInput= ((EditText)findViewById(R.id.edit_path_input)).getText().toString();
                List<String> listdata = new ArrayList<String>();
                listdata.add(pathInput);
                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,R.layout.filelist,listdata);
                listView.setAdapter(arrayAdapter);
            }
        });

        listView = (ListView) findViewById(R.id.list_item);

        //列表项单击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                TextView tvtime = (TextView) findViewById(R.id.video_file);
                Data.fileName = tvtime.getText().toString();
                File videoFile=new File(getExternalFilesDir("").toString()+"/"+Data.fileName);
                if(videoFile.exists()){
                    //获取视频时长
                    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
                    mmr.setDataSource(videoFile.getAbsolutePath());
                    String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                    Data.videoTime = Integer.parseInt(duration);
                    //跳转活动，播放视频
                    Intent intent = new Intent(MainActivity.this,VideoPlayer.class);
                    startActivity(intent);
                }
                else{
                    Toast.makeText(MainActivity.this,"此文件不存在",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

}
